package org.jeecg.modules.monitoring.j_monitoring.job.screen;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.ScreenData;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.SoftwareModuleSetting;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.SoftwareSetting;
import org.jeecg.modules.monitoring.j_monitoring.service.IScreenDataService;
import org.jeecg.modules.monitoring.j_monitoring.service.ISoftwareModuleSettingService;
import org.jeecg.modules.monitoring.j_monitoring.service.ISoftwareSettingService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * 同步用户手动维护的大屏数据
 * @Author: xiaoyq
 */

@Slf4j
public class SyncUserMaintenanceScreenDataJob implements Job {
	@Autowired
	private IScreenDataService screenDataService;

	@Autowired
	private ISoftwareSettingService softwareSettingService;

	@Autowired
	private ISoftwareModuleSettingService softwareModuleSettingService;

	private List<ScreenData> getScreenDataList(){
		List<ScreenData> dataList = null;
		DynamicDataSourceContextHolder.push("history");
		try{
			dataList = screenDataService.list();
		}
		finally {
			DynamicDataSourceContextHolder.clear();
		}

		return dataList;
	}

	private List<SoftwareSetting> getSoftwareSettingList(QueryWrapper<SoftwareSetting> queryWrapper){
		List<SoftwareSetting> dataList = null;
		DynamicDataSourceContextHolder.push("history");
		try{
			dataList = softwareSettingService.list(queryWrapper);
		}
		finally {
			DynamicDataSourceContextHolder.clear();
		}

		return dataList;
	}

	private List<SoftwareModuleSetting> getSoftwareModuleSettingList(QueryWrapper<SoftwareModuleSetting> queryWrapper){
		List<SoftwareModuleSetting> dataList = null;
		DynamicDataSourceContextHolder.push("history");
		try{
			dataList = softwareModuleSettingService.list(queryWrapper);
		}
		finally {
			DynamicDataSourceContextHolder.clear();
		}

		return dataList;
	}


	/***
	 * 执行
	 * @param jobExecutionContext
	 * @throws JobExecutionException
	 */
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info(String.format("同步用户手动维护的大屏数据,时间:" + DateUtils.getTimestamp()));

		List<ScreenData> dataList = getScreenDataList();
		try{
			screenDataService.saveOrUpdateBatch(dataList);

			QueryWrapper<SoftwareSetting> queryWrapper = new QueryWrapper();
			queryWrapper.select("max(update_time) update_time");
			SoftwareSetting lastEntity = softwareSettingService.getOne(queryWrapper);

			if(lastEntity!=null && lastEntity.getUpdateTime()!=null){
				queryWrapper.clear();
				queryWrapper.lambda().gt(SoftwareSetting::getUpdateTime,lastEntity.getUpdateTime());
				List<SoftwareSetting> softwareSettings = getSoftwareSettingList(queryWrapper);
				if(softwareSettings!=null && softwareSettings.size()>0){
					softwareSettingService.saveOrUpdateBatch(softwareSettings);
				}
			}

			QueryWrapper<SoftwareModuleSetting> softwareModuleSettingQueryWrapper = new QueryWrapper();
			softwareModuleSettingQueryWrapper.select("max(update_time) update_time");
			SoftwareModuleSetting lastModuleSetting = softwareModuleSettingService.getOne(softwareModuleSettingQueryWrapper);

			if(lastModuleSetting!=null && lastModuleSetting.getUpdateTime()!=null){
				softwareModuleSettingQueryWrapper.clear();
				softwareModuleSettingQueryWrapper.lambda().gt(SoftwareModuleSetting::getUpdateTime,lastModuleSetting.getUpdateTime());
				List<SoftwareModuleSetting> moduleSettings = getSoftwareModuleSettingList(softwareModuleSettingQueryWrapper);
				if(moduleSettings!=null && moduleSettings.size()>0){
					softwareModuleSettingService.saveOrUpdateBatch(moduleSettings);
				}
			}

		}
		finally {
			DynamicDataSourceContextHolder.clear();
		}
	}

}
